package test.log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.FormatFlagsConversionMismatchException;

public class SqlLog {
    public final static String SQL_END = ";\n";
    public final static long MAX_LOG_SIZE = 1024 * 1024 * 1024; // 1G
    // private BufferedWriter sqlLog;
    private FileWriter sqlLog = null;
    private String path;
    //供后期日志文件限定大小时， 自动切换为多个文件时使用
    private String preName; //文件名前缀
    private String extensionName; //文件扩展名
    private int logSeq = 0;
    private boolean autoSwitchFlag = false;
    private long msgLength = 0;

    public SqlLog(String preName, String extensionName) {
        this.preName = preName;
        this.extensionName = extensionName;
        this.path = String.format("%s.%s", preName, extensionName);
        try {
// sqlLog = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream(this. path, true)));
            sqlLog = new FileWriter(this.path);
        } catch (IOException e) {
            System.out.println("running sql log open failed!");
        }
    }

    //设置是否允许自动切换日志文件
    public void setAutoSwitch(boolean flag) {
        autoSwitchFlag = flag;
    }

    public void Close() {
        try {
            if (sqlLog != null) {
                sqlLog.flush();
                sqlLog.close();
                sqlLog = null;
            }
        } catch (Exception e) {
        }
    }

    public void Flush() {
        try {
            sqlLog.flush();
        } catch (IOException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public boolean reOpen() {
        try {
            Close();
// sqlLog = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream(this. path, true)));
            sqlLog = new FileWriter(this.path);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public boolean Clear() {
        try {
            Close();
            File tmp = new File(this.path);
            tmp.delete();
            tmp.createNewFile();
// sqlLog = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream(this. path, true)));
            sqlLog = new FileWriter(this.path);
            msgLength = 0;
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public synchronized void sqlLogMessage(String sqlMessage) {
        try {
            sqlLog.write(sqlMessage);
            sqlLog.flush();
            msgLength += sqlMessage.length();
            autoSwithLog();
        } catch (IOException e) {
        }
    }

    //切换新的日志文件，日志文件名增加序号以示区别
    public boolean switchLog() {
        boolean flag = false;
        try {
            Close();
            logSeq++;
            File oldFile = new File(path);
            String archPath = String.format("%s-%d.%s", preName, logSeq, extensionName);
            File archFile = new File(archPath);
            flag = oldFile.renameTo(archFile);
            sqlLog = new FileWriter(path);
            msgLength = 0;
        } catch (IOException e) {
        }
        return flag;
    }

    //自动根据文件大小限制切换为新的日志文件
    private void autoSwithLog() {
        if (autoSwitchFlag) {
            if (msgLength > MAX_LOG_SIZE) {
                switchLog();
            }
        }
    }
}
